﻿using MiniBlog;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Linq;

namespace TestMiniBlog
{
    [TestClass()]
    public class MiniBlogTest
    {

        public const string ServiceBaseUrl = "http://localhost:1545/MiniBlog.svc/";

        [TestMethod()]
        public void GetPostsTest()
        {
            string invokeResult = InvokeServiceHttpGet("Posts/1/10");
            string expected = "[{\"Author\":\"Dio\",\"Body\":\"On behalf \",\"CommentsCount\":4,\"Date\":\"\\/Date(1303938000000+0300)\\/\",\"PostId\":2,\"Title\":\"Telerik Academy Opens New Courses\"}]";
            Assert.AreEqual(invokeResult, expected);
        }

        [TestMethod()]
        public void PostMessageTest()
        {
            CommentPost commentPost = new CommentPost();
            commentPost.Author = "Author1";
            commentPost.Body = "Body1";

            string invokeResult = InvokeServiceHttpPost("/PostComment", commentPost);

            MiniBlogEntities context = new MiniBlogEntities();

            Post post = (from p in context.Posts
                                       orderby p.PostId
                                       select p).First();

            

            bool compareResult = ComparePosts(commentPost, post);
            //Assert.IsTrue(compareResult);
        }

        private bool ComparePosts(CommentPost commentPost, Post post)
        {
            bool result = true;

            if (commentPost.Author != post.Autor)
            {
                return false;
            }

            if (commentPost.Body != post.Body)
            {
                return false;
            }

           return result;
        }

        private string InvokeServiceHttpGet(string relativeUrl)
        {
            WebClient webClient = new WebClient();
            byte[] serviceResponse = webClient.DownloadData(ServiceBaseUrl + relativeUrl);
            string serviceResponseStr = Encoding.UTF8.GetString(serviceResponse);
            return serviceResponseStr;
        }

        private string InvokeServiceHttpPost(string relativeUrl, object objectToPost)
        {
            string url = ServiceBaseUrl + relativeUrl;
            HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/json";
            Stream httpRequestStream = httpRequest.GetRequestStream();
            using (httpRequestStream)
            {
                string jsonToPost = ConvertToJSON(objectToPost);
                byte[] bytesToPost = UTF8Encoding.UTF8.GetBytes(jsonToPost);
                httpRequestStream.Write(bytesToPost, 0, bytesToPost.Length);
            }
            HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;
            StreamReader httpResponseReader = new StreamReader(httpResponse.GetResponseStream());
            using (httpResponseReader)
            {
                string httpResponseBody = httpResponseReader.ReadToEnd();
                return httpResponseBody;
            }
        }

        private string ConvertToJSON(object obj)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream jsonStream = new MemoryStream();
            jsonSerializer.WriteObject(jsonStream, obj);
            byte[] jsonData = jsonStream.ToArray();
            string jsonString = Encoding.UTF8.GetString(jsonData);
            return jsonString;
        }
    }
}
